<?php

namespace app\api\controller;

use app\api\model\UserCoupon as UserCouponModel;
use app\api\model\Merchant as MerchantModel;

/**
 * 商户中心
 * Class Coupon
 * @package app\api\controller
 */
class Merchant extends Controller
{

	/* @var UserCouponModel $model */
    private $model;

    /* @var \app\api\model\User $model */
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function initialize()
    {
        //parent::initialize();
        //$this->user = $this->getUser();//商户总信息
        $this->usermer = $this->getShangUser();   // 商户信息
        $this->imgadess = $this->pcUrl();
    }





    /**
    商家资料
     **/
    public  function  merchantsel(){
         $list = $this->usermer;
        if(!empty($list['logo'])){
            $list['logoad'] = $this->imgadess.$list['logo'].'.jpg';
        }

        if($list['is_delete'] == '3' || $list['is_delete'] == '1'){
            $list['delete_name1'] = '未认证';
            $list['delete_name2'] = '休息中';
        }else{
            $list['delete_name1'] = '已认证';
            $list['delete_name2'] = '休息中';
            if($list['is_delete'] == '2'){
                $list['delete_name2'] = '营业中';
            }
        }
        return $this->renderSuccess( 1000,'成功',$list);

    }

    /**
    修改商家资料
     **/
    public  function  merchantup()
    {
        $info = $this->request->post();
        $info['id'] = $this->usermer['merchant_id'];
        if(!empty($info['logo'])){
            $upda['logo'] = $info['logo'];
        }
        if(!empty($info['merchant_name'])){
            $upda['merchant_name'] = $info['merchant_name'];
        }
        if(!empty($info['jieshao'])){
            $upda['jieshao'] = $info['jieshao'];
        }
        if(!empty($info['is_delete'])){
            $upda['is_delete'] = $info['is_delete'];
        }
        if(!empty($upda)){
            $model = new MerchantModel();
            if($model->updatemen($info['id'],$upda)){
                return $this->renderSuccess( '1000','成功');
            }
        }else{
            return $this->renderError(1001,'无修改');
        }
        return $this->renderError(1002,'失败');

    }



    /*
     * 我的设计师
     * */
    public function shejisList()
    {
        $info = $this->request->post();
        if(empty($this->usermer) || $info['tates'] !='1'){
            return $this->renderError(1001,'不是装修公司');
        }
        $model = new MerchantModel();
        if(empty($info['types'])){
            $info['types'] = '1';
        }
        $list = $model->selTuanDui($this->usermer['merchant_id'],$info['types']);
        if(count($list['data'])){
            foreach ($list['data'] as $key=>$value){
                if(!empty($value['logo'])){
                    $list['data'][$key]['logoad'] = $this->imgadess.$value['logo'].'.jpg';
                }else{
                    $list['data'][$key]['logoad'] = '';
                }
            }
        }
        return $this->renderSuccess( 1000,'成功',$list);
    }

    /*
     * 工地list
     * */
    public function gongdiList()
    {
        $info = $this->request->post();
        if(empty($this->usermer) || $info['tates'] !='1'){
            return $this->renderError(1001,'不是装修公司');
        }
        $model = new MerchantModel();
        $list = $model->gongdiList($this->usermer['merchant_id']);
        if(count($list['data'])){
            foreach ($list['data'] as $key=>$value){
                if(!empty($value['gongdi_log'])){
                    $list['data'][$key]['logoad'] = $this->imgadess.$value['gongdi_log'].'.jpg';
                }else{
                    $list['data'][$key]['logoad'] = '';
                }
            }
        }
        return $this->renderSuccess( 1000,'成功',$list);
    }

    /*
     * 工地list
     * */
    public function gongdiOne()
    {
        $info = $this->request->post();
        if(empty($this->usermer) || $info['tates'] !='1'){
            return $this->renderError(1001,'不是装修公司');
        }
        if(empty($info['id'])){
            return $this->renderError(1002,'参数不能为空');
        }
        $model = new MerchantModel();
        $list = $model->gongdione($this->usermer['merchant_id'],$info['id']);
        if(!empty($list)){
            if(!empty($list['gongdi_log'])){
                $list['gongdi_logad'] = $this->imgadess.$list['gongdi_log'].'.jpg';
            }else{
                $list['gongdi_logad'] = '';
            }
            $list['gongdi_zhulist'] = $model->gongdimerone($list['gongdi_zhu']);
            if(!empty($list['gongdi_zhulist']['logo'])){
                $list['gongdi_zhulist']['logoad'] = $this->imgadess.$list['gongdi_zhulist']['logo'].'.jpg';
            }
            $gongdi_shelist = $model->gongdimer($list['gongdi_she']);
            if(count($gongdi_shelist)){
                foreach ($gongdi_shelist as $k1 =>$v1){
                    if(!empty($v1['logo'])){
                        $gongdi_shelist[$k1]['logoad'] = $this->imgadess.$v1['logo'].'.jpg';
                    }else{
                        $gongdi_shelist[$k1]['logoad'] = '';
                    }
                }
            }
            $gongdi_gonglist = $model->gongdimer($list['gongdi_gong']);
            if(count($gongdi_gonglist)){
                foreach ($gongdi_gonglist as $k2 =>$v2){
                    if(!empty($v2['logo'])){
                        $gongdi_gonglist[$k2]['logoad'] = $this->imgadess.$v2['logo'].'.jpg';
                    }else{
                        $gongdi_gonglist[$k2]['logoad'] = '';
                    }
                }
            }
            $list['gongdi_shelist'] = $gongdi_shelist;
            $list['gongdi_gonglist'] = $gongdi_gonglist;
        }
        return $this->renderSuccess( 1000,'成功',$list);
    }



}